#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title01_1最长连续子序列.py 
@version:
@time: 2023/05/27 
"""


def solve(ints, sum_):
    max_len = 0
    for i in range(len(ints)):
        tmp_sum = 0
        sub_len = 0
        for j in range(i, len(ints)):
            if tmp_sum > sum_:
                break
            tmp_sum += ints[j]
            sub_len += 1
            if tmp_sum == sum_:
                max_len = max(sub_len, max_len)
    max_len = -1 if max_len == 0 else max_len
    return max_len


ints = list(map(int, input().split(',')))
sum_ = int(input())
print(solve(ints, sum_))
